<?php
namespace addon\cms\logic;
use app\common\logic\LogicBase;
use \tpfcore\util\Tree;
use tpfcore\Core;
use tpfcore\helpers\StringHelper;
use think\Db;
use \think\Config;

class Spage extends LogicBase
{
	public function getSpage($where=[]){
		return self::getList($where);
	}
	public function saveSpage($data){
		$validate_posts=Core::addonValidate($this->name);
		$validate_result = $validate_posts->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $validate_posts->getError(), null];
        }

        if($data['make_to_html']==1){
        	if(empty($data['filename'])){
        		return [RESULT_ERROR, "html文件路径必须", null];
	        }else{
	        	$filename_arr = explode(".", $data['filename']);
	        	$endwith = end($filename_arr);
		        if($endwith!="html" && $endwith!="htm"){
		        	return [RESULT_ERROR, "请以htm或html结尾", null];
		        }
	        }
    	}else{
    		unset($data['filename']);
    	}

    	if(empty($data['sign']) && $data['ssign'])
    	{
    		$data['sign'] = $data['ssign'];
    	}
        
        $last_id=Core::loadAddonModel($this->name)->saveObject($data);
        
        if($last_id){

        	$last_id=empty($data['id'])?$last_id:$data['id'];

        	if(!empty($data['filename'])){

        		$dir_path = str_replace(basename($data['filename']), "", $data['filename']);

        		if(!is_dir($dir_path)){
				    $flag = @mkdir($dir_path,0777,true);
				}
				file_put_contents($data['filename'], file_get_contents(SITE_PATH.url("/cms/Vpage/index/pageid/$last_id")));
        	}

	        return [RESULT_SUCCESS, '操作成功', url("cms/Spage/index")];
        }else{
        	return [RESULT_ERROR, '操作失败', null];
        }
	}

	public function delSpage($data){
		if(Core::loadAddonModel($this->name)->deleteObject(['id'=>$data['id']],true)){
			return [RESULT_SUCCESS, '删除成功', url("cms/Spage/index")];	
		}else{
			return [RESULT_ERROR, '删除失败', url("cms/Spage/index")];
		}
	}
	
}